package com.example.scaffold_demo.http.reponse;


import com.example.httplibrary.exception.ExceptionHandle;
import com.example.scaffold_demo.bean.BaseBean;
import com.example.scaffold_demo.common.Constant;
import com.example.scaffold_demo.utils.SPUtils;
import com.example.scaffold_demo.utils.StringUtil;


import androidx.annotation.NonNull;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Author: 李巷阳
 * Date: 2020/6/1
 * Description: http响应处理类
 */
public class RxHttpReponseCompat {


    public static <T> ObservableTransformer<BaseBean<T>, T> compatResult() {


        return new ObservableTransformer<BaseBean<T>, T>() {
            @Override
            public ObservableSource<T> apply(Observable<BaseBean<T>> baseBeanObservable) {

                return baseBeanObservable.flatMap(new Function<BaseBean<T>, ObservableSource<T>>() {
                    @Override
                    public ObservableSource<T> apply(@NonNull final BaseBean<T> tBaseBean) throws Exception {

                        String jtime_point = tBaseBean.getTime_point();
                        // 如果联网状态下,保存服务器时间.否则认为是缓存时间,不需要保存到本地
                        // 判断不是缓存的message,则重新设置本地时间
                        if (jtime_point != null) {
                            saveTimeDifference(jtime_point);
                        }
                        if (tBaseBean.isSuccess()) {
                            return Observable.create(new ObservableOnSubscribe<T>() {
                                @Override
                                public void subscribe(ObservableEmitter<T> subscriber) throws Exception {

                                    try {
                                        subscriber.onNext(tBaseBean.getData());
                                        subscriber.onComplete();
                                    } catch (Exception e) {
                                        subscriber.onError(e);
                                    }
                                }
                            });
                        } else {
                            return Observable.error(new ExceptionHandle.ResponeThrowable(tBaseBean.getCode(), tBaseBean.getMessage()));
                        }

                    }
                }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io());
            }
        };

    }

    // 添加本地与服务端的时间差
    public static void saveTimeDifference(String jtime_point) {
        long server_side_time = Long.valueOf(jtime_point).longValue();
        long local_time = StringUtil.getCurrenTimestampSecond();
        long time_difference = local_time - server_side_time;
        SPUtils.save(Constant.TIME_DIFFERENCE, time_difference);
    }

}
